import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'models/collect_configuration.dart';
import 'stripe_terminal_usb_platform_interface.dart';

/// An implementation of [StripeTerminalUsbPlatform] that uses method channels.
class MethodChannelStripeTerminalUsb extends StripeTerminalUsbPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('stripe_terminal_usb');

  @override
  Future<String?> getPlatformVersion() async {
    final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
    return version;
  }
  /*
    获取权限
   */
  @override
  Future<bool> permissionAllowed() async{
    return await methodChannel.invokeMethod('permissionAllowed');
  }

  /*
  初始化 并连接读卡器
   */
  @override
  Future<void> init(String localId,String tokenUrl,String departId) async{
    await methodChannel.invokeMethod<bool>('init',{
      "localId":localId,
      "tokenUrl":tokenUrl,
      "departId":departId,
    });
  }
  /*
   打开钱箱
   */
  @override
  Future<bool> initOpenCash() async {
    return await methodChannel.invokeMethod("initOpenCash");
  }
  /*
   打开钱箱
   */
  @override
  Future<Map> openCash() async {
    Map map = await methodChannel.invokeMethod("openCash");
    return map;
  }
  /*
   连接读卡器
   */
  @override
  Future<void> connectReaders() async {
    await methodChannel.invokeMethod("connectReaders");
  }

  /*
  支付
   */
  @override
  Future<Map> pay(String clientSecret, {
    /// Configuration for the collection process
    CollectConfiguration? collectConfiguration = const CollectConfiguration(
      skipTipping: true,
    ),
  }) async{
    Map map = await methodChannel.invokeMethod('pay',{
      "clientSecret":clientSecret,
      "collectConfiguration": collectConfiguration?.toMap(),
    });
    return map;
  }
  /*
  订单捕获
   */
  @override
  Future<Map> processPayment() async{
    Map map = await methodChannel.invokeMethod('processPayment');
    return map;
  }

  /*
  取消等待刷卡状态
   */
  @override
  Future<bool> cancelRetrievePaymentIntent() async {
    bool cancel = await methodChannel.invokeMethod("cancelRetrievePaymentIntent");
    return cancel;
  }

  /*
  取消付款意向
   */
  @override
  Future<Map> cancelProcessPayment() async {
    Map map = await methodChannel.invokeMethod("cancelProcessPayment");
    return map;
  }
}
